package net.findkrypton.event;

import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import net.findkrypton.enume.Level;
import net.findkrypton.po.LoadSave;
import net.findkrypton.po.SaveFile;
import net.findkrypton.po.SaveTableData;
import net.jumpengine.M;
import net.jumpengine.Manager;
import net.jumpengine.bean.Base;
import net.jumpengine.bean.BoxBase;
import net.jumpengine.bean.ButtonBase;
import net.jumpengine.bean.ImageBase;
import net.jumpengine.bean.TableBase;
import net.jumpengine.bean.TextRect;
import net.jumpengine.bean.TimeBase;
import net.jumpengine.enume.GameState;
import net.jumpengine.inter.IAction;
import net.jumpengine.inter.IStateEvent;
import net.jumpengine.inter.ITimeHandle;
import net.jumpengine.manager.ImageManager;
import net.jumpengine.po.Border;
import net.jumpengine.po.Column;

public class LoadStateEvent implements IStateEvent{
	private ImageBase back;
	private boolean load=false;
	private TableBase<SaveFile> tableBase;
	private SaveTableData<SaveFile> data;
	private TextRect tip;
	private TimeBase time;

	@Override
	public void setBackImage(Image image) {
		if(back==null) {
			back=new ImageBase(0, 0,-10, image);
			M.i().add(back);
		}else {
			back.setImage(image);
		}
	}

	@Override
	public boolean isLoaded() {
		return load;
	}

	@Override
	public void load() {
		ImageManager im = Manager.getImageManager();
		
		Font f=Font.font("黑体", FontWeight.BOLD, 20);
		
		BoxBase box=new BoxBase(294, 155, 655, 500, -1, null, Color.grayRgb(100));
		im.add(box);
		
		TextRect title = new TextRect(304, 165, 632, 35, "读 取 存 档", new Border());
		title.setFont(f);
		title.setColor(Color.grayRgb(220));
		title.setBackColor(Color.web("#990000"));
		im.add(title);
		
		tableBase = new TableBase<>(304, 200, 632, 2);
		tableBase.addColumn(new Column("序", true))
		.addColumns(
			new Column("修改时间", 228),
			new Column("难度", 58),
			new Column("宽", 44),
			new Column("高", 44),
			new Column("已用时间", 105),
			new Column("未标记氪金", 112)
		);
		LoadSave.load();
		data=new SaveTableData<SaveFile>(LoadSave.getList());
		tableBase.setTableData(data);
		tableBase.setEnum(Level.class, new String[] {"入门","普通","困难"});
		im.add(tableBase);
		
		ButtonBase btu=new ButtonBase(684,605, 100, 40, new Border(2,2,4,5,20), "读取");
		btu.setFont(f);
		btu.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				int selRow = tableBase.getSelRow();	
				if(selRow==-1) {
					tip.setVisible(true);
					time.setAble(true);
					return;
				}
				String msg = LoadSave.loadData(selRow);
				if(msg==null) {
					Manager.setState(GameState.running);
					return;
				}
				tip.setText(msg);
				tip.setVisible(true);
				time.setAble(true);
			}
		});
		im.add(btu);
		
		Font font=new Font("黑体", 20);
		tip=new TextRect(364,605,250,40,"请选择要读取的存档!");
		tip.setFont(font);
		tip.setColor(Color.WHITE);
		tip.setBackColor(Color.rgb(118,82,15));
		
		time=new TimeBase(3000, tip, new ITimeHandle() {
			@Override
			public void timeHandle(TimeBase time, Base source) {
				source.setVisible(false);
				time.setAble(false);
			}
		});
		tip.setVisible(false);
		time.setAble(false);
		im.add(tip);
		im.addUpdate(time);
		
		btu=new ButtonBase(818,605, 100, 40, new Border(2,2,4,5,20), "取消");
		btu.setFont(f);
		btu.setAction(new IAction() {
			@Override
			public void handle(int x, int y, MouseButton button, Base base) {
				Manager.beforeState();
			}
		});
		im.add(btu);
		load=true;
	}

	@Override
	public void show() { }

	@Override
	public void pause() { }

	@Override
	public void end() { }
}
